package com.cskaoyan.service.admin.system;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.auth.Md5Util;
import com.cskaoyan.exception.InvalidParamException;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * @author erdonc
 * @date 2022/9/7 9:14
 * @description: TODO
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    /**
     * @description: TODO 根据管理员用户名进行模糊查询
     * @date: 2022/9/7 15:19
     * @param: basePageInfo 分页信息
     * @param: name 管理员名称
     */
    @Override
    public CommonData<MarketAdmin> list(BasePageInfo info, String name) {
        // 开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        // 设置查询条件
        MarketAdminExample example = new MarketAdminExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (name != null) {
            criteria.andUsernameLike("%" + name + "%");
        }
        List<MarketAdmin> admins = marketAdminMapper.selectListByExample(example);
        PageInfo<MarketAdmin> pageInfo = new PageInfo<>(admins);
        return CommonData.data(pageInfo);
    }


    /**
     * @description: TODO 添加管理员信息
     * @date: 2022/9/7 17:15
     * @param: [admin]
     */
    @Override
    public MarketAdmin create(MarketAdmin admin) {
        // 校验用户名是否重复
        checkName(admin);
        // 设置添加时间和修改时间
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        // 对密码加密
        String password = admin.getPassword();
        try {
            String md5Pwd = Md5Util.getMd5(password);
            admin.setPassword(md5Pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        marketAdminMapper.insertSelective(admin);
        // 获取自增id，传入，进行查询，然后返回响应信息
        MarketAdmin marketAdmin = marketAdminMapper.selectByPrimaryKey(admin.getId());
        return marketAdmin;
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/12 20:51
     * TODO:  修改管理员
     */
    @Override
    public MarketAdmin update(MarketAdmin admin) {
        // 校验用户名是否重复
        checkName(admin);
        // admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        // 对密码加密
        String password = admin.getPassword();
        try {
            String md5Pwd = Md5Util.getMd5(password);
            admin.setPassword(md5Pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        marketAdminMapper.updateByPrimaryKeySelective(admin);
        MarketAdmin updateAdmin = marketAdminMapper.selectByPrimaryKey(admin.getId());
        return updateAdmin;
    }

    /**
     * @description: TODO 删除管理员
     * @date: 2022/9/8 16:30
     * @param: [admin]
     */
    @Override
    public void delete(MarketAdmin admin) {
        marketAdminMapper.deleteByPrimaryKey(admin.getId());
    }


    /**
     * @description: TODO 校验用户名是否重复
     * @date: 2022/9/7 17:20
     * @param: [admin]
     */
    private void checkName(MarketAdmin admin) {
        // 从数据库中查询管理员用户名，判断是否重复
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(admin.getUsername());
        criteria.andDeletedEqualTo(false);

        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        if (marketAdmins.size() > 0 && !marketAdmins.get(0).getId().equals(admin.getId())) {
            throw new InvalidParamException("用户名已存在");
        }


    }
}
